package com.zd.kids.ui.activity;

import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.hyy.libray.presenter.IBasePresenter;
import com.hyy.libray.ui.activity.BaseActivity;
import com.zd.kids.DataBean.TrainTypeDataBean;
import com.zd.kids.DataBean.TrainTypeListDataBean;
import com.zd.kids.R;
import com.zd.kids.common.GlideUtil;
import com.zd.kids.net.CustomObserver;
import com.zd.kids.net.HttpMethod;
import com.zd.kids.ui.adapter.TrainTypeListAdapter;
import com.zd.kids.ui.view.CommmonBottomHelper;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class TrainTypeListActivity extends BaseActivity {


    TrainTypeListAdapter trainTypeListAdapter;

    @BindView(R.id.recycle_view)
    RecyclerView recycleView;

    @BindView(R.id.ll_rootview)
    LinearLayout llRootview;

    @BindView(R.id.img_back)
    ImageView img_back;

    @BindView(R.id.top_img)
    ImageView topImg;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.tv_content_name)
    TextView tvName;


    private String mUnitId;

    public static final String UNIT_ID_KEY = "UNIT_ID_KEY";

    /**
     * 获取布局文件地址
     */
    @Override
    public int getLayoutResId() {
        return R.layout.activity_train_type_list;
    }

    /**
     * 初始化布局
     */
    @Override
    public void initView() {
        ImmersionBar.with(this)
                .fullScreen(true)
                .hideBar(BarHide.FLAG_HIDE_BAR)
                .init();

        CommmonBottomHelper commmonBottomHelper = new CommmonBottomHelper(this, llRootview);

        img_back.setOnClickListener(view ->
                this.finish()
        );
    }

    @Override
    public void initData() {
        super.initData();

        mUnitId = getIntent().getStringExtra(UNIT_ID_KEY);

        trainTypeListAdapter = new TrainTypeListAdapter(new ArrayList<>());
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setAdapter(trainTypeListAdapter);


    }

    @Override
    protected void onResume() {
        super.onResume();
        getTrainList();
    }

    /**
     * 获取Presenter，不用MVP，可以为空
     *
     * @return
     */
    @Override
    protected IBasePresenter onBasePresenter() {
        return null;
    }

    private void getTrainList() {
        showLoading();
        HttpMethod.getIntence().getTrainList(mUnitId, new CustomObserver<TrainTypeDataBean>(this) {
            @Override
            public void onSuccess(TrainTypeDataBean trainTypeDataBean) {
                disLoading();
                List<TrainTypeListDataBean> trainTypeListDataBeans = trainTypeDataBean.getResult();

                for (int i = 0; i < trainTypeListDataBeans.size(); i++) {
                    TrainTypeListDataBean trainTypeListDataBean = trainTypeListDataBeans.get(i);

                    if (i == 0) {
                        trainTypeListDataBean.setType_name(getString(R.string.interactive_video));
                        trainTypeListDataBean.setImgRes(R.mipmap.ic_train_type_1);
                    } else if (i == 1) {
                        trainTypeListDataBean.setType_name(getString(R.string.read_storybooks));
                        trainTypeListDataBean.setImgRes(R.mipmap.ic_train_type_2);
                    } else if (i == 2) {
                        trainTypeListDataBean.setType_name(getString(R.string.words_practice));
                        trainTypeListDataBean.setImgRes(R.mipmap.ic_train_type_3);
                    } else if (i == 3) {
                        trainTypeListDataBean.setType_name(getString(R.string.reading_assessment));
                        trainTypeListDataBean.setImgRes(R.mipmap.ic_train_type_4);
                    }

                }
                trainTypeListAdapter.setNewData(trainTypeListDataBeans);


                tvTitle.setText(trainTypeDataBean.getUnit_name());

                tvName.setText(trainTypeDataBean.getQuestion_name());

                GlideUtil.load(trainTypeDataBean.getQuestion_img(), topImg, R.mipmap.ic_train_type_top_default);

            }

            @Override
            public void onError(String errorCode) {
                disLoading();
            }
        });
    }
}
